package com.xtaller.sdboot.core.base;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.Serializable;

/**
 * @author : Taller
 * @date : 2020/1/15 00:10
 * @Description :
 */
@Slf4j
public class BaseController <E extends BaseEntity> extends RestController {

    /***
     * 查询ViewObject的分页数据
     * <p>
     * url请求参数示例: /list?rows=1&page=1&orderBy=createTime:asc&where=school:like:2
     * </p>
     * @return
     * @throws Exception
     */
    @GetMapping("/list")
    public Object getByPage(HttpServletRequest request) throws Exception{
        return getPage(convertParams2Map(request));
    }

    /***
     * 根据资源id查询ViewObject
     * @param id ID
     * @return
     * @throws Exception
     */
    @GetMapping("/{id}")
    public Object getById(@PathVariable("id")Serializable id, HttpServletRequest request) throws Exception{
        return get(id, request);
    }

    /***
     * 创建资源对象
     * @param entity
     * @return JsonResult
     * @throws Exception
     */
    @PostMapping("")
    public Object createEntity(@Valid @RequestBody E entity, HttpServletRequest request) throws Exception {
        return create(entity, request);
    }

    /***
     * 根据ID更新资源对象
     * @param entity
     * @return JsonResult
     * @throws Exception
     */
    @PutMapping("/{id}")
    public Object updateEntity(@PathVariable("id") Serializable id, @Valid @RequestBody E entity, HttpServletRequest request) throws Exception {
        return update(id, entity, request);
    }

    /***
     * 根据id删除资源对象
     * @param id
     * @return
     * @throws Exception
     */
    @DeleteMapping("/{id}")
    public Object deleteEntity(@PathVariable("id")Serializable id, HttpServletRequest request) throws Exception {
        return delete(id, request);
    }

}
